package com.zk5_education.service_edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zk5_education.config.ExceptionHandle.MyException;
import com.zk5_education.service_edu.entity.EduCourse;
import com.zk5_education.service_edu.entity.EduCourseDescription;
import com.zk5_education.service_edu.entity.vo.AllCourseQuery;
import com.zk5_education.service_edu.entity.vo.CourseMessage;
import com.zk5_education.service_edu.entity.vo.CourseQuery;
import com.zk5_education.service_edu.mapper.EduCourseMapper;
import com.zk5_education.service_edu.service.EduCourseDescriptionService;
import com.zk5_education.service_edu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-08-08
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    EduCourseDescriptionService descriptionService;

    @Override
    public String addcourse(CourseQuery courseQuery) {
        EduCourse ec=new EduCourse();
        BeanUtils.copyProperties(courseQuery,ec);
        int insert = baseMapper.insert(ec);
        if(insert<=0){
            throw new MyException(20001,"添加失败");
        }
        String cid=ec.getId();
        EduCourseDescription ed=new EduCourseDescription();
        BeanUtils.copyProperties(courseQuery,ed);
        ed.setId(cid);
        descriptionService.save(ed);
        return cid;
    }

    @Override
    public CourseQuery getcourseMessage(String courseid) {

      CourseQuery cq=new CourseQuery();
        EduCourse eduCourse = baseMapper.selectById(courseid);
        BeanUtils.copyProperties(eduCourse,cq);

        EduCourseDescription byId = descriptionService.getById(courseid);
        BeanUtils.copyProperties(byId,cq);
        return cq;
    }

    @Override
    public void saveCourse(CourseQuery courseQuery) {
        //修改课程信息
        EduCourse ec=new EduCourse();
        EduCourseDescription ed=new EduCourseDescription();
        BeanUtils.copyProperties(courseQuery,ec);
        BeanUtils.copyProperties(courseQuery,ed);
        baseMapper.updateById(ec);
        descriptionService.save(ed);
    }
//最终发布信息回显
    @Override
    public CourseMessage selectMessage(String id) {
        CourseMessage courseMessage = baseMapper.seleteCourseMessage(id);
        return courseMessage;
    }

    @Override
    public void publishcourse(String courseid) {
        EduCourse course =new EduCourse();
        course.setId(courseid);
        course.setStatus("Normal");
        baseMapper.updateById(course);
    }

    @Override
    public void pageCourse(Page<EduCourse> page, AllCourseQuery allCourseQuery) {
        String status = allCourseQuery.getStatus();
        String title = allCourseQuery.getTitle();
        QueryWrapper<EduCourse> qw=new QueryWrapper<>();
        if(allCourseQuery==null){
            baseMapper.selectPage(page,qw);
        }
        if(!StringUtils.isEmpty(status)){
            qw.eq("status",status);
        }
        if(!StringUtils.isEmpty(title)){
            qw.like("title",title);
        }
          baseMapper.selectPage(page, qw);
    }

    @Override
    public void deleteCourse(String courseid) {

    }
}
